package com.cc8w.home.controller;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;

import com.cc8w.util.PageData;

public class BaseController {
	protected HttpServletRequest request=null;
    protected HttpServletResponse response=null;
    protected Model model=null;
    protected String path="";
    protected  String basePath = "";
    protected  String selvetPath = "";



    @ModelAttribute("attributeAdminInit")
    public void adminInit(HttpServletRequest request, HttpServletResponse response,Model model) {
        System.out.println("每次请求都执行AdminController");
        this.setRequest(request);
        this.setResponse(response);


        //项目名称
        this.path = request.getContextPath();
        //项目网址加项目名称
        this.basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + this.path + "/";
        //请求地址的路径名
        this.selvetPath = request.getServletPath();
        model.addAttribute("APP_PATH", this.path);
        model.addAttribute("BASE_PATH", this.basePath);
        model.addAttribute("SELVET_PATH", this.selvetPath);

        this.setModel(model);


    }

    public HttpServletRequest getRequest() {
        return request;
    }


    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }


    public HttpServletResponse getResponse() {
        return response;
    }


    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }


    public Model getModel() {
        return model;
    }


    public void setModel(Model model) {
        this.model = model;
    }

    /**
     * 得到PageData
     */
    public PageData getPageData(){
        return new PageData(this.getRequest());
    }

    public String jump(String page,HashMap<String,String> hm) {

//        obj.put("title", "系统提示");
//        obj.put("msg", "添加失败！");
//        obj.put("num", "3");
//        obj.put("back_url", this.basePath+"admin/product/typelist");

        if(!hm.containsKey("title")) hm.put("title","系统提示");
        if(!hm.containsKey("msg")) hm.put("msg","在操作！");
        if(!hm.containsKey("num")) hm.put("num","3"); //跳转倒计时
        if(!hm.containsKey("back_url")) hm.put("back_url",""); //跳转返回页面

        Iterator<Entry<String, String>> entries  = hm.entrySet().iterator();
        while(entries.hasNext()){

            Entry<String, String> entry = entries.next();
            String key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+":"+value);
            this.model.addAttribute(key,value);
        }

        return page;
    }
    /**
     * 用于处理api接口异常的
     * @return
     */
    @ExceptionHandler({})
    public String exception(Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        return "exception";
    }

}
